import request from "../request";
import { SmsLoginData, UserInfoVo } from "./loginTypes";
import { LoginData, LoginResult, VerifyCodeResult } from "./loginTypes";
import { PromiseResponse } from "@/service/api/type.ts";

/**
 * @param data {LoginData}
 * @returns
 */
function login(data: LoginData): PromiseResponse<LoginResult> {
  const params = {
    phoneNumber: data.phoneNumber.trim(),
    password: data.password,
    code: data.code,
    uuid: data.uuid,
  };

  return request({
    url: "/auth/login",
    headers: {
      isToken: false,
    },
    method: "post",
    data: params,
  });
}

function smsLogin(data: SmsLoginData): PromiseResponse<LoginResult> {
  const params = {
    phoneNumber: data.phoneNumber.trim(),
    smsCode: data.smsCode,
  };

  return request({
    url: "/auth/smsLogin",
    headers: {
      isToken: false,
    },
    method: "post",
    data: params,
  });
}

// 管理端不提供注册
// 注册方法
// export function register(data: any) {
//   return request({
//     url: '/auth/register',
//     headers: {
//       isToken: false
//     },
//     method: 'post',
//     data: data
//   });
// };

/**
 * 注销
 */
function logout(): PromiseResponse<any> {
  return request({
    url: "/auth/logout",
    method: "post",
  });
}

/**
 * 获取图形验证码
 */
function getCaptchaImage(): PromiseResponse<VerifyCodeResult> {
  return request({
    url: "/captcha/image/code",
    headers: {
      isToken: false,
    },
    method: "get",
    timeout: 20000,
  });
}

/**
 * 申请登录短信验证码
 */
function requestLoginSmsCaptcha(
  phoneNumber: string,
  captchaVO: object,
): PromiseResponse<any> {
  return request({
    url: "/auth/apply",
    headers: { isToken: false },
    method: "post",
    data: {
      phoneNumber,
      // 登录验证码
      codeType: "0",
      captchaVO,
    },
  });
}

/**
 * 获取用户详细信息
 */
function getUserInfo(): PromiseResponse<UserInfoVo> {
  return request({
    url: "/system/user/getInfo",
    method: "get",
  });
}

export {
  login,
  smsLogin,
  logout,
  getCaptchaImage,
  requestLoginSmsCaptcha,
  getUserInfo,
};
